const app = getApp();
var CONFIG = require("../../../config/config.js");
var share = require('../../../utils/share.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        is_xuancate: 1,
        area_team: [], //校区列表
        area_index: 0, //校区下标
        area_id: '', //校区下标
        is_area_more: 1,
        is_scorder: 1, //1没有查询课程 2查询课程
        keycourse: '',
        is_search: 1, //1搜索 2选择
        steparr: [{ id: 1, title: '1.课程分类选择' }, { id: 2, title: '2.选择课程' }],
        is_stepid: 1,
        regin1: '',
        regin2: '',
        is_searchid: 1, //1分类搜索 2查询
        courseregin1: '', //课程
        multiIndex: [-1, -1], //课程分类下标
        multiArray: [], //课程分类数组
        chooselist: [], //课程分类二级列表
        courseregin1: '', //课程
        courseregin2: '', //课程
        courseIndex: -1, //课程下标
        courseArray: [], //课程数组
        courselist: [], //当前规格数组
        course_title: '', //课程选中name
        cart_title: '',
        cate_id: '', //分类id
        cate_id1:'',
        course_id: '', //课程id
        // 查询课程
        keywordpeople: '',
        tap_id: 1,
        taparr: [{ id: 1, title: '已选期次' }, { id: 2, title: '未选期次' }],
        page: 0,
        courselistpeople: [],
        coursepeoplefalse: false,
        is_test:0,
        tests:[{id:1,name:'是'},{id:2,name:'否'}],
        testitem:'',
        testindex:'',
        chenorderfalse:false,
    },
    repeatclosefree(){
      this.setData({
        chenorderfalse:false,
      })
    },
    radioclick(e){
      var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
          is_test:item.id
        })
    },
    suretest(){
        var that = this;
        if(Number(that.data.is_test)>=1){
          wx.showLoading({
              title: '加载中~',
          })
          var objdata = {
            tel:that.data.testitem.phone,
            is_test:that.data.is_test,
          }
          share.wxRequest('crmapi/user/upderp', objdata, function(res) {
            wx.hideLoading();
            if (Number(res.data.errcode) == 0) {
              wx.showToast({
                  title: '修改成功',
                  icon: 'none'
              })
              that.data.courselistpeople[that.data.testindex].is_test = that.data.is_test;
              that.setData({
                courselistpeople:that.data.courselistpeople,
                chenorderfalse:false,
              })
            }else{
              wx.showToast({
                  title:res.data.msg,
                  icon: 'none'
              })
            }
          })
        }else{
          wx.showToast({
              title: '请选择是否通过考试',
              icon: 'none'
          })
        }
    },
    xiustudentsclick(e){
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        var index = e.currentTarget.dataset.index || e.target.dataset.index;
        if(Number(item.is_test)>=1){
          var is_test = item.is_test;
        }else{
          var is_test = 0;
        }
        that.setData({
          testitem:item,
          testindex:index,
          is_test:is_test,
          chenorderfalse:true,
        })
    },
    allstudentsclick(e){
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        wx.navigateTo({
          url: '/pages/apply/find/newstudents/allstudents?class_id='+item.id+'&title='+item.course_name+'',
        })
    },
    getdetail(e){
        console.log(e);
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        wx.setStorageSync('studentInfo', JSON.stringify(item));
        wx.navigateTo({
            url: '/pages/apply/skill/studentdesc',
        })
    },
    clicktap(e) {
        var that = this;
        var item = e.target.dataset.item || e.currentTarget.dataset.item;
        that.setData({
            tap_id: item.id,
            page: 0,
        })
        that.courseClass();
    },
    // 点击查询订单
    ordersclick() {
        var that = this;
        var courseIndex = that.data.courseIndex;
        if (Number(courseIndex) <= -1) {
            wx.showToast({
                title: '请选择课程',
                icon: 'none'
            })
        } else {
            that.setData({
                is_scorder: 2
            })
            that.courseClass();
        }
    },
    courseClass() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var objdata={}
        var url=''
        if(that.data.cate_id1==162){
            objdata = {
                keyword: that.data.keywordpeople, //	否	string	关键字
                consultant_id: app.globalData.admin_id, //	是	int	当前管理员ID
                course_id: that.data.course_id, //	是	int	课程ID
                // is_change: that.data.tap_id, //	否	int	0-全部 1-已选班 2-未选班
                page: that.data.page, //	否	int	默认0
            }
            url='crmapi/user/stuListJn'

        }else{
            objdata = {
                keyword: that.data.keywordpeople, //	否	string	关键字
                consultant_id: app.globalData.admin_id, //	是	int	当前管理员ID
                course_id: that.data.course_id, //	是	int	课程ID
                is_change: that.data.tap_id, //	否	int	0-全部 1-已选班 2-未选班
                page: that.data.page, //	否	int	默认0
            }
            url='crmapi/user/courseClass'

        }
        share.wxRequest(url, objdata, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                if (Number(that.data.page) <= 0) {
                    var list = data;
                } else {
                    var list = that.data.courselistpeople.concat(data)
                }
                that.setData({
                    courselistpeople: list
                })
            }
            that.setData({
                coursepeoplefalse: true,
            })
            wx.hideLoading();
        })
    },

    // 点击搜索课程名称
    formSubmit(e) {
        var that = this;
        var keycourseall = e.detail.value.keycourseall;
        that.areaCourse(keycourseall)
    },
    keyclick(e) {
        var that = this;
        var item = e.target.dataset.item;
        that.areaCourse(item.name)
    },
    // 选择课程分类
    catefirstChange(e) {
        var that = this;
        var index = e.target.dataset.index;
        var item = e.target.dataset.item;
        if (item.child && Number(item.child.length) > 0) {
            var child = item.child;
            var cate_id = '';
            var is_xuancate = 1;
            var is_stepid = 1;
        } else {
            var child = [];
            var cate_id = item.id;
            var is_xuancate = 2;
            var is_stepid = 2;
        }
        var cate_id1=item.id
        that.setData({
            regin1: item.title,
            chooselist: item.child,
            "multiIndex[0]": index,
            "multiIndex[1]": -1,
            cate_id: cate_id,
            cate_id1: cate_id1,
            cart_title: item.title,
            courseIndex: -1,
            courseregin1: '',
            courseregin2: '',
            course_title: '',
            is_xuancate: is_xuancate,
            is_stepid: is_stepid,
        })
        if (cate_id != '') {
            that.cateCourse();
        }
    },
    // /选择二级分类
    catesecChange(e) {
        var that = this;
        var index = e.target.dataset.index;
        var item = e.target.dataset.item;
        var cart_title = that.data.cart_title + ',' + item.title;
        that.setData({
            cart_title: cart_title,
            regin2: item.title,
            cate_id: item.id,
            "multiIndex[1]": index,
            is_xuancate: 2,
            is_stepid: 2,
        })
        that.cateCourse();
    },
    // 选择课程
    choosecourseclick(e) {
        var that = this;
        var index = e.target.dataset.index || e.currentTarget.dataset.index;
        var item = e.target.dataset.item || e.currentTarget.dataset.item;
        that.setData({
            course_id: item.id,
            course_title: item.title,
            courseregin1: item.title,
            courseIndex: index,
        })
        if (Number(that.data.is_searchid) == 2) {
            if (item.pptitle != '') {
                var cart_title = item.ptitle + ',' + item.pptitle
            } else {
                var cart_title = item.ptitle
            }
            that.setData({
                cate_id: item.pid,
                cart_title: cart_title,
            })
        }
    },
    // 获取所选课程
    cateCourse() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        that.setData({
            courseArray: [],
            courselist: [],
        })
        var formData = {
            pid: that.data.cate_id,
            area_id: that.data.area_id,
            // user_id: that.data.uc.user_id,
        }
        share.wxRequest('crmapi/user/cateCourse2', formData, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var newarr = [];
                for (var i = 0; i < data.length; i++) {
                    newarr.push(data[i]);
                }
                that.setData({
                    courseArray: newarr,
                    courselist: newarr,
                })
                if (Number(data.length) < 1) {
                    wx.showToast({
                        title: '该分类下暂无课程',
                        icon: 'none'
                    })
                }
            }
            wx.hideLoading();
        })
    },
    catedelChange(e) {
        var that = this;
        var index = e.target.dataset.index || e.currentTarget.dataset.index;
        if (Number(index) == 1) {
            that.setData({
                regin1: '',
                regin2: '',
                chooselist: [],
                "multiIndex[0]": -1,
                "multiIndex[1]": -1,
                cate_id: '',
                cate_id1:'',
                cart_title: '',
                courseIndex: -1,
                courseregin1: '',
                courseregin2: '',
                course_title: '',
                is_xuancate: 1,
                is_stepid: 1,
            })
        } else if (Number(index) == 2) {
            that.setData({
                cart_title: that.data.regin1,
                regin2: '',
                cate_id: '',
                cate_id1:'',
                "multiIndex[1]": -1,
                is_xuancate: 1,
                is_stepid: 1,
                courseIndex: -1,
                courseregin1: '',
                courseregin2: '',
                course_title: '',
            })
        } else if (Number(index) == 3) {
            that.setData({
                courselist: [],
                discount_name: '',
                pay_type: '',
                downpayment: '',
                course_id: '',
                discountsId: '',
                discountsIndex: -1,
                y_price: '',
                course_title: '',
                courseregin1: '',
                courseIndex: -1,
                is_stepid: 2,
            })
        }
        that.setData({
            is_scorder: 1,
        })
    },
    areaCourse(name) {
        var that = this;
        if (name == '') {
            wx.showToast({
                title: '请输入关键字',
                icon: 'none'
            })
        } else {
            wx.showLoading({
                title: '加载中~',
            })
            share.wxRequest('crmapi/user/queryCourse', {
                keyword: name,
                area_id: that.data.area_id,
                // user_id: that.data.uc.user_id,
                limit: 100
            }, function(res) {
                if (Number(res.data.errcode) == 0) {
                    var data = res.data.data;
                    if (Number(data.length) <= 0) {
                        wx.showToast({
                            title: '该搜索下暂无课程',
                            icon: 'none'
                        })
                    } else {
                        that.setData({
                            courseArray: data,
                            is_search: 1,
                            is_stepid: 2,
                            is_searchid: 2,
                        })
                    }
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
                wx.hideLoading();
            })
        }
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.teamlist();
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },
    // 校区选择
    areaeChange(e) {
        var that = this;
        // console.log(e)
        var index = e.target.dataset.index;
        that.setData({
            area_index: index,
        })

        if (Number(index) > 0) {
            that.setData({
                area_id: that.data.area_team[index].id
            })
            that.getAreaCourse();

        } else {
            that.setData({
                area_id: ''
            })
        }
    },
    // 获取课程分类
    getAreaCourse() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        that.setData({
            multiIndex: [-1, -1], //课程分类下标
            multiArray: [], //课程分类数组
            chooselist: [],
            regin1: '',
            regin2: '',
        })
        share.wxRequest('crmapi/User/cateListQuery', { area_id: that.data.area_id }, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data1 = res.data.data;
                that.setData({
                    multiArray: data1,
                })
            }
            wx.hideLoading();
        })
    },
    closeareae() {
        var that = this;
        that.setData({
            area_index: 0,
            area_id: '',
            is_scorder: 1,
            regin1: '',
            regin2: '',
            chooselist: [],
            "multiIndex[0]": -1,
            "multiIndex[1]": -1,
            cate_id: '',
            cate_id1:'',
            cart_title: '',
            courseIndex: -1,
            courseregin1: '',
            courseregin2: '',
            course_title: '',
            is_xuancate: 1,
            is_stepid: 1,
        })
    },
    areaeChangemore() {
        var that = this;
        that.setData({
            is_area_more: 2
        })
    },
    areaeChangemore1() {
        var that = this;
        that.setData({
            is_area_more: 1
        })
    },
    // 获取校区列表
    teamlist() {
        var that = this;
        share.wxRequest('crmapi/User/team', {}, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj = [{
                    id: 0,
                    title: '请选择校区'
                }]
                var data1 = obj.concat(data);
                that.setData({
                    area_team: data1
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})